<?php

namespace app\http\middleware;

use EasyWeChat\Factory;
use think\Controller;
use think\Db;

class Wechat extends Controller
{
    public function handle($request, \Closure $next)
    {
        if (!session('?wechat')){
            $wechatConfig = config('wechat.');
            $param = $request->param();
            $paramStr = '';
            if (!empty($param)){
                foreach ($param as $k=>$v){
                    $paramStr .= $k.'='.$v;
                }
            }
            $url = $request->module().','.$request->controller().','.$request->action().','.$paramStr;
            $wechatConfig['oauth']['callback'] = url('Login/authCallback',['url'=>$url],'',true);
            $app = Factory::officialAccount($wechatConfig);
            $app->oauth->scopes(['snsapi_userinfo'])
                ->redirect()->send();
        }

        return $next($request);
    }
}
